{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./tensorflow/mnist/input_data/train-images-idx3-ubyte.gz\n",
      "Extracting ./tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz\n",
      "Extracting ./tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting ./tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "data_dir = './tensorflow/mnist/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Current accuracy: ', 0.1092)\n",
      "('Current accuracy: ', 0.9673)\n",
      "('Current accuracy: ', 0.9716)\n",
      "('Current accuracy: ', 0.97670001)\n",
      "('Current accuracy: ', 0.98049998)\n",
      "('Current accuracy: ', 0.98100001)\n",
      "('Current accuracy: ', 0.9806)\n",
      "('Current accuracy: ', 0.98159999)\n",
      "('Current accuracy: ', 0.98259997)\n",
      "('Current accuracy: ', 0.98360002)\n",
      "('Final accuracy: ', 0.98360002)\n"
     ]
    }
   ],
   "source": [
    "x = tf.placeholder(tf.float32,[None,784])\n",
    "\n",
    "#权重初始化为uniform,设置隐层神经元数量为300\n",
    "W1 = tf.Variable(tf.random_uniform([784, 300], -0.01, 0.01))\n",
    "b1 = tf.Variable(tf.zeros([300]))\n",
    "#用relu作为激活函数\n",
    "y1 = tf.nn.relu(tf.matmul(x,W1)+b1)\n",
    "\n",
    "#增加了一个隐层\n",
    "W2 = tf.Variable(tf.random_uniform([300, 10], -0.01, 0.01))\n",
    "b2 = tf.Variable(tf.zeros([10]))\n",
    "y2 = tf.matmul(y1,W2)+b2 #y2 = tf.nn.softmax(tf.matmul(y1, W2) + b2)\n",
    "\n",
    "y = y2\n",
    "y_ = tf.placeholder(tf.float32,[None,10])\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y))\n",
    "\n",
    "#L2正则化\n",
    "regularizer = tf.contrib.layers.l2_regularizer(0.0001)\n",
    "regularization = regularizer(W1) + regularizer(W2) + regularizer(b1) + regularizer(b2)\n",
    "\n",
    "loss = cross_entropy + regularization\n",
    "\n",
    "#设置学习率为0.6\n",
    "train_step = tf.train.GradientDescentOptimizer(0.6).minimize(loss)\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)\n",
    "\n",
    "for _ in range(10000):\n",
    "    if _%1000 == 0:\n",
    "        correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "        print('Current accuracy: ',sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))\n",
    "\n",
    "    \n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys})\n",
    "    \n",
    "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "print('Final accuracy: ',sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
